package DBLayer;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.LinkedList;

import ModelLayer.*;

public class DBActivityType implements IFDBActivityType {
	private Connection con;

	public DBActivityType() {
		con = DBConnection.getInstance().getDBcon();
	}

	@Override
	public ActivityType getActivity(long ID) {
		String wClause = "  ID = '" + ID + "'";
		return singleWhere(wClause);
	}

	@Override
	public ArrayList<ActivityType> getActivities() {
		return new ArrayList<ActivityType>(miscWhere(""));
	}

	@Override
	public int insertActivity(ActivityType activity) {
		int rc = -1;

		activity.setID(GetMaxId.getMaxId("Select max(ID) from activityType") + 1);

		String query = "INSERT INTO activityType(ID, name, groupActivity, minParticipant, maxParticipant, priceFacility, priceInstructor) VALUES('"
				+ activity.getID()
				+ "','"
				+ activity.getName()
				+ "','"
				+ activity.isGroupActivity()
				+ "','"
				+ activity.getMinParticipants()
				+ "','"
				+ activity.getMaxParticipants()
				+ "','"
				+ activity.getPricePerHour_Facility()
				+ "','"
				+ activity.getPricePerHour_Instructor() + "')";
		System.out.println("insert : " + query);
		try {
			Statement stmt = con.createStatement();
			stmt.setQueryTimeout(5);
			rc = stmt.executeUpdate(query);
			stmt.close();
			con.commit();
		} catch (Exception e) {
			System.out
					.println("[ERROR] DBLayer.ActivityType - New entry not inserted: "
							+ e.getMessage());
		}
		return rc;
	}

	@Override
	public int updateActivity(long ID, ActivityType activity) {
		int rc = -1;
		String query = "UPDATE activityType SET " + "name = '"
				+ activity.getName() + "'," + "groupActivity = '"
				+ activity.isGroupActivity() + "'," + "minParticipant = '"
				+ activity.getMinParticipants() + "'," + "maxParticipant = '"
				+ activity.getMaxParticipants() + "'," + "priceFacility = '"
				+ activity.getPricePerHour_Facility() + "',"
				+ "priceInstructor = '" + activity.getPricePerHour_Instructor()
				+ "'" + " WHERE ID = '" + ID + "'";
		System.out.println("Update query:" + query);
		try {
			Statement stmt = con.createStatement();
			stmt.setQueryTimeout(5);
			rc = stmt.executeUpdate(query);
			stmt.close();
		} catch (Exception e) {
			System.out.println("[ERROR] DBLayer.DBActivityType - Entry #" + ID
					+ " was not updated.\n " + e.getMessage());
		}
		return rc;
	}

	@Override
	public int removeActivity(long ID) {
		int rc = -1;
		String query = "DELETE FROM activityType WHERE ID = '" + ID + "'";
		System.out.println(query);
		try {
			Statement stmt = con.createStatement();
			stmt.setQueryTimeout(5);
			rc = stmt.executeUpdate(query);
			stmt.close();
		} catch (Exception e) {
			System.out.println("[ERROR] DBLayer.DBActivityType - Entry #" + ID
					+ " was not deleted. \n" + e.getMessage());
		}
		return rc;
	}

	private LinkedList<ActivityType> miscWhere(String wClause) {
		ResultSet results;
		LinkedList<ActivityType> list = new LinkedList<ActivityType>();
		String query = buildQuery(wClause);
		try {
			Statement stmt = con.createStatement();
			stmt.setQueryTimeout(5);
			results = stmt.executeQuery(query);

			while (results.next()) {
				ActivityType at = new ActivityType();
				at = buildActivityType(results);
				list.add(at);
			}
			stmt.close();
		} catch (Exception e) {
			System.out.println("Query exception - select: " + e);
			e.printStackTrace();
		}
		return list;
	}

	private ActivityType singleWhere(String wClause) {
		ResultSet results;
		ActivityType at = new ActivityType();
		String query = buildQuery(wClause);
		try {
			Statement stmt = con.createStatement();
			stmt.setQueryTimeout(5);
			results = stmt.executeQuery(query);

			if (results.next()) {
				at = buildActivityType(results);
				stmt.close();
			} else {
				at = null;
			}
		} catch (Exception e) {
			System.out.println("Query exception: " + e);
		}
		return at;
	}

	private ActivityType buildActivityType(ResultSet results) {
		ActivityType at = new ActivityType();
		try {
			at.setID(results.getLong("ID"));
			at.setName(results.getString("name"));
			at.setGroupActivity(results.getBoolean("groupActivity"));
			at.setMinParticipants(results.getInt("minParticipant"));
			at.setMaxParticipants(results.getInt("maxParticipant"));
			at.setPricePerHour_Facility(results.getDouble("priceFacility"));
			at.setPricePerHour_Instructor(results.getDouble("priceInstructor"));
		} catch (Exception e) {
			System.out
					.println("[ERROR] DBLayer.DBActivityType - error in building object: "
							+ e.getMessage());
		}
		return at;
	}

	private String buildQuery(String wClause) {
		String query = "SELECT * FROM activityType"; // ID, name, groupActivity,
														// minParticipants,
														// maxParticipants,
														// priceFacility,
														// priceInstructor

		if (wClause.length() > 0)
			query = query + " WHERE " + wClause;

		return query;
	}
}
